package com.snail.common.dyd;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
@Slf4j
public class DynamicDatabaseProcessAspect {

    private AspectDynamicDataBaseProcessor dynamicDataBaseProcessor;

    public DynamicDatabaseProcessAspect(AspectDynamicDataBaseProcessor dynamicDataBaseProcessor){
        this.dynamicDataBaseProcessor = dynamicDataBaseProcessor;
    }

    @Pointcut("execution(public * com.snail.common.dyd.DynamicDataBase+.*(..))")
    public void collectorPoint(){

    }

    @Around("collectorPoint()")
    public Object methodAroundPorcess(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        log.info("开始执行数据源切换");
        dynamicDataBaseProcessor.switchDataBase(proceedingJoinPoint);
        log.info("数据源切换结束");
        return proceed(proceedingJoinPoint);
    }



    private Object proceed(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        Object[] args = proceedingJoinPoint.getArgs();
        return proceedingJoinPoint.proceed(args);
    }

}
